package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.text.method.QwertyKeyListener;
import android.text.method.TextKeyListener.Capitalize;
import android.util.Log;
import android.view.*;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.SkinCommonData;
import com.lzx.iteam.bean.SkinHorizData;
import com.lzx.iteam.keyboard.Environment;
import com.lzx.iteam.keyboard.InputModeSwitcher;
import com.lzx.iteam.keyboard.SkbContainer;
import com.lzx.iteam.keyboard.SoftKey;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.widget.DialpadEditText;
import com.lzx.iteam.widget.HInterceptFrameLayout;
import com.lzx.iteam.widget.HInterceptFrameLayout2;
import com.lzx.iteam.widget.TextImageButton;

public class KeyboardHoriFrag extends Fragment implements
    View.OnClickListener, OnTouchListener, View.OnKeyListener,
	View.OnLongClickListener {
    private static final boolean D = true;

    private static final int COMMON_VIEW_INIT = R.layout.key_hori;

    private LinearLayout mT9Board;
    private boolean mIsT9 = true;
    private boolean mIsDisplayUnderPanel = true;

    private TextView mBtnHDialTextShowArea;

    private View mDisplayBottomView;

    private HInterceptFrameLayout mKeyboardHorizontalBottom;
    private HInterceptFrameLayout2 mKeyboardeHorizonalHandler;


    // 是否显示t9 or allwords keyboard
    public TextImageButton mDisplayUnderPanelButton;
    private TextImageButton mDisplayT9orWords;

    private DialpadEditText mDigits_h;
    private DialpadEditText mDigits_h_words;

    public static final int KEY_IS_INIT = R.id.dial_buttons_T9;

    // 横向T9键盘
    private TextImageButton mView_h_num1;
    private TextImageButton mView_h_num2;
    private TextImageButton mView_h_num3;
    private TextImageButton mView_h_num4;
    private TextImageButton mView_h_num5;
    private TextImageButton mView_h_num6;
    private TextImageButton mView_h_num7;
    private TextImageButton mView_h_num8;
    private TextImageButton mView_h_num9;
    private TextImageButton mView_h_num0;
    private TextImageButton mView_h_num_del;
    private TextImageButton mView_h_num_star;
    private TextImageButton mView_h_num_dial;
    private TextImageButton mView_h_num_sms;

    private SkbContainer mQwertyBoard;
    private InputModeSwitcher mInputModeSwitcher;
    private Environment mEnvironment;

    private Animation mAnimationVerticalAllwordVisible;
    private Animation mAnimationVerticalAllwordGone;

    private PreferenceUtil mPreferenceUtil;
    private CloudDialerActivity mActivity;

    public SkinHorizData mSkin;
    public SkinCommonData mSkinCommon;
    private View mKeyHori;
    private int mIsHaveTone;
    private boolean mNeedReloadSkin;

    public static KeyboardHoriFrag newInstance(Bundle args) {
        KeyboardHoriFrag f = new KeyboardHoriFrag();
        f.setArguments(args);
        return f;
    }

    public static KeyboardHoriFrag newInstance() {
        KeyboardHoriFrag f = new KeyboardHoriFrag();
        return f;
    }


    private static final String TAG = "KeyboardHoriFrag";

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (D) Log.d(TAG, "onAttach: ");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (D) Log.d(TAG, "onCreate: ");

        mActivity = (CloudDialerActivity) getActivity();
        mPreferenceUtil = PreferenceUtil.getInstance(mActivity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (D) Log.d(TAG, "onDetach: ");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (D) Log.d(TAG, "onCreateView: ");
        mIsT9 = mPreferenceUtil.getBoolean(PreferenceUtil.IS_T9, true);
        mIsHaveTone = mPreferenceUtil.getInt(PreferenceUtil.CONTACT_IS_HAVESOUND,
                Constants.SOUND_CLOSE);

        mKeyHori = inflater.inflate(R.layout.key_hori, container, false);
        setupRootViewInKeyboard();

        mSkin = ((CloudDialerActivity)getActivity()).mHorizSkin;
        mSkinCommon = ((CloudDialerActivity)getActivity()).mSkinCommon;

        switchKeyboard(mIsT9, mSkin, mSkinCommon, false);

        Bundle activityArgs = getArguments();
        if (activityArgs.containsKey(CloudDialerActivity.DATA)) {
            formatDigitInput((Intent) activityArgs.getParcelable(CloudDialerActivity.DATA));
            activityArgs.remove(CloudDialerActivity.DATA);
        }
        String input = activityArgs.getString("input");
        if(input != null){
            setFilterText(input);
        }
        return mKeyHori;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (D) Log.d(TAG, "onViewCreated: ");
    }

    @Override
    public void onStart() {
        super.onStart();
        if (D) Log.d(TAG, "onStart: ");
    }

    @Override
    public void onResume() {
        super.onResume();
        if (D) Log.d(TAG, "onResume: ");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (D) Log.d(TAG, "onHiddenChanged: ");
    }

    public void changeSkin(SkinHorizData skin, SkinCommonData commonData) {
        mSkinCommon = commonData;
        mSkin = skin;
        initCommonViewSkin(skin, commonData);
            initHoriT9Skin(skin, commonData);
            initHoriQwertySkin(skin, commonData);
    }

    public void formatDigitInput(Intent i) {
        if(i == null){
            return;
        }
        String action = i.getAction();
        Uri data = i.getData();
        if (Intent.ACTION_VIEW.equals(action)) {
            Uri uri = data;
            if (uri != null && "tel".equals(uri.getScheme())) {
                // Put the requested number into the input area
                String dataStr = uri.getSchemeSpecificPart();
                changeKeyBoard2FitDial();
                setFormattedDigits(dataStr);
            }
        } else if (Intent.ACTION_DIAL.equals(action)) {
            Uri uri = data;
            if (uri != null && "tel".equals(uri.getScheme())) {
                // Put the requested number into the input area
                String dataStr = uri.getSchemeSpecificPart();
                changeKeyBoard2FitDial();
                setFormattedDigits(dataStr);
            }
        }
    }

    protected void setFormattedDigits(String data) {
        // strip the non-dialable numbers out of the data string.
        String dialString = PhoneNumberUtils.extractNetworkPortion(data);
        dialString = PhoneNumberUtils.formatNumber(dialString);
        if (!TextUtils.isEmpty(dialString)) {
            setFilterText(dialString);
        }
    }

    public void changeKeyBoard2FitDial() {
        if (!mIsT9) {
            mIsT9 = true;
            switchKeyboard(mIsT9, mSkin, mSkinCommon, false);
//            mPreferenceUtil.save(PreferenceUtil.IS_T9_KEYBOARD_STATUS, mIsT9_hort);
        }
    }

    public void switchKeyboard(boolean isT9, SkinHorizData skin, SkinCommonData commonData, boolean animGone) {
        Object tag = null;

        mPreferenceUtil.save(PreferenceUtil.IS_T9, isT9);

        setupCommonView(mKeyHori, skin, commonData);
        if (isT9) {
            tag = mT9Board.getTag(KEY_IS_INIT);
            if (tag == null) {
                mT9Board.setTag(KEY_IS_INIT, new Object());

                mDigits_h.addTextChangedListener(mActivity.mTextWatcher);
                mDigits_h_words.setVisibility(View.GONE);

                initKeypadHorizental();
                initHoriT9Skin(skin, mSkinCommon);
            } else if (mNeedReloadSkin) {
                mNeedReloadSkin = false;
            }
            initHoriT9Skin(skin, mSkinCommon);
            mDisplayT9orWords.setText("ABC▲");
            mDigits_h.setVisibility(View.VISIBLE);
            mDigits_h_words.setVisibility(View.GONE);
            if (!animGone) {
                mQwertyBoard.setVisibility(View.GONE);
                mT9Board.setVisibility(View.VISIBLE);
//                mActivity.freshSearchDisplay();
            }

            mDigits_h.requestFocus();

        } else {
            tag = mQwertyBoard.getTag(KEY_IS_INIT);
            if (tag == null) {
                mQwertyBoard.setTag(KEY_IS_INIT, new Object());

                final QwertyKeyListener qwertyKeyListener = QwertyKeyListener.getInstance(false,
                        Capitalize.NONE);
                mDigits_h_words.addTextChangedListener(mActivity.mTextWatcherQwerty);
                mDigits_h_words.setKeyListener(qwertyKeyListener);

                setupKeypadHorizentalAllwords();
                initHoriQwertySkin(skin, commonData);
            } else if (mNeedReloadSkin) {
                mNeedReloadSkin = false;
            }
            initHoriQwertySkin(skin, commonData);
            mDisplayT9orWords.setText("123▼");
            mDigits_h_words.setVisibility(View.VISIBLE);
            mDigits_h.setVisibility(View.GONE);
            if (!animGone) {
                mT9Board.setVisibility(View.GONE);
                mQwertyBoard.setVisibility(View.VISIBLE);
//                mActivity.freshSearchDisplay();
            }
            mDigits_h_words.requestFocus();
        }

        mKeyHori.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                initFilterText();
                mKeyHori.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });
    }

    public boolean getIsT9() {
        return mIsT9;
    }

    public String getFilterText() {
        String filterText = "";
        if (mIsT9 && mDigits_h != null) {
            filterText = mDigits_h.getText().toString();
        } else if (mDigits_h_words != null) {
            filterText = mDigits_h_words.getText().toString();
        }
        return filterText;
    }

    public String getNumberFilterText(){
        String result = "";
        if(mIsT9 && mDigits_h != null){
            result = mDigits_h.getText().toString();
        }
        return result;
    }

    public void initFilterText() {
        if (D) Log.d(TAG, "initFilterText: " + mActivity.mInputTextT9 + " " + mActivity.mInputTextQwerty);
        if (mIsT9) {
            mDigits_h.setText(mActivity.mInputTextT9);
            mDigits_h.setSelection(
                    mActivity.mInputTextT9 != null ? mActivity.mInputTextT9.length() : 0);
        } else {
            mDigits_h_words.setText(mActivity.mInputTextQwerty);
            mDigits_h_words.setSelection(
                    mActivity.mInputTextQwerty != null ? mActivity.mInputTextQwerty.length() : 0);
        }
    }

    public void setFilterText(String text) {
        Log.d(TAG, "setFilterText H", new Exception());
        if (mIsT9) {
            if(mDigits_h != null){
                mDigits_h.setText(text);
                mDigits_h.setSelection(text.length());
            }
        } else {
            if(mDigits_h_words != null){
                mDigits_h_words.setText(text);
                mDigits_h_words.setSelection(text.length());
            }
        }
    }

    private void setupRootViewInKeyboard() {
        Log.i("doubleS","xxxx");
        mQwertyBoard = (SkbContainer) mKeyHori.findViewById(R.id.skbcontainer);
        mT9Board = (LinearLayout) mKeyHori.findViewById(R.id.dial_pad_under);

        mKeyboardHorizontalBottom = (HInterceptFrameLayout) mKeyHori.findViewById(
                R.id.keypad_horizonal_bottom);
        mKeyboardHorizontalBottom.setOnDoubleScrollListener(new HInterceptFrameLayout.DoubleScrollListener() {
            @Override
            public void onDoubleScroll(float dis) {
                Log.i("doubleSs",dis+"");
                mActivity.scrollHoriPadView(dis);
            }

            @Override
            public void onDobleScrollUp() {
                mActivity.saveHoriKbLastPosition();
            }
        });
        mKeyboardeHorizonalHandler = (HInterceptFrameLayout2) mKeyHori.findViewById(
                R.id.keypad_horizonal_handler);
        mKeyboardHorizontalBottom.setListener(mActivity);
        mKeyboardeHorizonalHandler.setListener(mActivity);

        mDigits_h = (DialpadEditText) mKeyHori.findViewById(R.id.digits_h);
        mDigits_h_words = (DialpadEditText) mKeyHori.findViewById(R.id.digits_h_allword);
        mDigits_h.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP
                        && !mIsDisplayUnderPanel) {
//                    mDisplayUnderPanelButton.setText("▼");
                    mQwertyBoard.setVisibility(View.GONE);
                    mT9Board.setVisibility(View.VISIBLE);
                    mIsDisplayUnderPanel = !mIsDisplayUnderPanel;
                }
                return true;
            }
        });
        mDigits_h_words.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Bug fix for htc 2.3.3 of wanglin's device
                if (event.getAction() == MotionEvent.ACTION_UP
                        && !mIsDisplayUnderPanel) {
//                    mDisplayUnderPanelButton.setText("▼");
                    mT9Board.setVisibility(View.GONE);
                    mQwertyBoard.setVisibility(View.VISIBLE);
                    mIsDisplayUnderPanel = !mIsDisplayUnderPanel;
                }
                return true;
            }
        });
    }

    private void setupKeypadHorizentalAllwords() {

        mEnvironment = Environment.getInstance();
        mEnvironment.onConfigurationChanged(getResources().getConfiguration(), getActivity());
        mInputModeSwitcher = new InputModeSwitcher(getActivity());

        //: vert keyboard
//        mQwertyBoard.setDmg(mActivity.dmg);
        mQwertyBoard.setInputConnection(new SkbContainer.InputConnection() {

            @Override
            public void responseSoftKeyEvent(SoftKey key) {
                if (mIsHaveTone > 0 && mSkin != null) {
//                    mActivity.playPhone(mActivity.player, mSkinCommon.def_tock, mActivity.tockPlayerId, null);
                }

                mQwertyBoard.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                keyPressed(key.getKeyCode());
            }

            @Override
            public void responseSoftKeyLongclick(SoftKey key) {
                //: heihei
                // we only use the method as a temp solution.
                // for we will change all the keyboard into the same
                // implementation
                if (null == mDigits_h_words) {
                    return;
                }

                if (mDigits_h_words.getText().length() > 0) {
                    mDigits_h_words.setText("");
                }

                mDigits_h_words.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            }
        });
        mQwertyBoard.setInputModeSwitcher(mInputModeSwitcher);
        mQwertyBoard.updateInputMode();
    }

    private void initKeypadHorizental() {
        View keyboardRoot = mT9Board;

        mView_h_num0 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_0);
        mView_h_num1 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_1);
        mView_h_num2 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_2);
        mView_h_num3 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_3);
        mView_h_num4 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_4);
        mView_h_num5 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_5);
        mView_h_num6 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_6);
        mView_h_num7 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_7);
        mView_h_num8 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_8);
        mView_h_num9 = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_num_h_9);

        mView_h_num_star = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_h_xing);
        mView_h_num_del = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_h_delete);
        mView_h_num_dial = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_h_dial);
        mView_h_num_sms = (TextImageButton) keyboardRoot.findViewById(R.id.dial_btn_h_send_msg);
        mBtnHDialTextShowArea = (TextView) keyboardRoot.findViewById(R.id.dial_btn_h_showarea);

        mView_h_num0.setOnClickListener(this);
        mView_h_num0.setOnTouchListener(this);
        mView_h_num0.setOnLongClickListener(this);

        mView_h_num1.setOnClickListener(this);
        mView_h_num1.setOnTouchListener(this);
        mView_h_num1.setOnLongClickListener(this);

        mView_h_num2.setOnClickListener(this);
        mView_h_num2.setOnTouchListener(this);
        mView_h_num2.setOnLongClickListener(this);

        mView_h_num3.setOnClickListener(this);
        mView_h_num3.setOnTouchListener(this);
        mView_h_num3.setOnLongClickListener(this);

        mView_h_num4.setOnClickListener(this);
        mView_h_num4.setOnTouchListener(this);
        mView_h_num4.setOnLongClickListener(this);

        mView_h_num5.setOnClickListener(this);
        mView_h_num5.setOnTouchListener(this);
        mView_h_num5.setOnLongClickListener(this);

        mView_h_num6.setOnClickListener(this);
        mView_h_num6.setOnTouchListener(this);
        mView_h_num6.setOnLongClickListener(this);

        mView_h_num7.setOnClickListener(this);
        mView_h_num7.setOnTouchListener(this);
        mView_h_num7.setOnLongClickListener(this);

        mView_h_num8.setOnClickListener(this);
        mView_h_num8.setOnTouchListener(this);
        mView_h_num8.setOnLongClickListener(this);

        mView_h_num9.setOnClickListener(this);
        mView_h_num9.setOnTouchListener(this);
        mView_h_num9.setOnLongClickListener(this);

        mView_h_num_star.setOnLongClickListener(this);
        mView_h_num_star.setOnClickListener(this);
        mView_h_num_star.setOnTouchListener(this);

        mView_h_num_del.setOnLongClickListener(this);
        mView_h_num_del.setOnClickListener(this);
        mView_h_num_del.setOnTouchListener(this);

        mView_h_num_dial.setOnLongClickListener(this);
        mView_h_num_dial.setOnClickListener(this);

        mView_h_num_sms.setOnClickListener(this);
        mView_h_num_sms.setOnLongClickListener(this);
    }

    private void setupCommonView(View v, SkinHorizData skin, SkinCommonData commonData) {
        if (v.getTag(COMMON_VIEW_INIT) == null) {
            mDisplayT9orWords = (TextImageButton) v.findViewById(R.id.btn_displayt9orwords);
            mDisplayUnderPanelButton = (TextImageButton) v.findViewById(R.id.isdisplay_dialpadunder);
            mDisplayBottomView = v.findViewById(R.id.display_bottom);
            initCommonViewSkin(skin, commonData);
            v.setTag(COMMON_VIEW_INIT, new Object());
        } else if (mNeedReloadSkin) {
            initCommonViewSkin(skin, commonData);
        }
    }

    private void initCommonViewSkin(SkinHorizData skin, SkinCommonData commonData) {
        mDisplayT9orWords.setOnClickListener(this);
        mDisplayT9orWords.setScaleType(commonData.scaleType);
        mDisplayT9orWords.setImageDrawable(skin.hAndAbcResId);
        mDisplayT9orWords.setColor(commonData.barTitleColor);
        mDisplayT9orWords.setBackgroundColor(commonData.backgroundColor);

        mDisplayUnderPanelButton.setOnClickListener(this);
        mDisplayUnderPanelButton.setScaleType(commonData.scaleType);
        mDisplayUnderPanelButton.setImageDrawable(skin.hidet9);
        mDisplayUnderPanelButton.setColor(commonData.barTitleColor);

        mDisplayBottomView.setBackgroundColor(commonData.backgroundColor);
    }

    public void initHoriQwertySkin(SkinHorizData skin, SkinCommonData commonData) {
        mDigits_h_words.setBackgroundDrawable(skin.hInputResId);
        mDigits_h_words.setTextColor(commonData.barTitleColor);
    }

    public void initHoriT9Skin(SkinHorizData skin, SkinCommonData skinCommonData) {
        mT9Board.setBackgroundColor(skinCommonData.backgroundColor);

        mDigits_h.setBackgroundDrawable(skin.hInputResId);
        mDigits_h.setTextColor(skinCommonData.barTitleColor);

        CloudDialerActivity.setBgAndScalType(mView_h_num0, skinCommonData);
        mView_h_num0.setImageDrawable(skin.ht9BgResId[0]);

        CloudDialerActivity.setBgAndScalType(mView_h_num1, skinCommonData);
        mView_h_num1.setImageDrawable(skin.ht9BgResId[1]);

        CloudDialerActivity.setBgAndScalType(mView_h_num2, skinCommonData);
        mView_h_num2.setImageDrawable(skin.ht9BgResId[2]);

        CloudDialerActivity.setBgAndScalType(mView_h_num3, skinCommonData);
        mView_h_num3.setImageDrawable(skin.ht9BgResId[3]);

        CloudDialerActivity.setBgAndScalType(mView_h_num4, skinCommonData);
        mView_h_num4.setImageDrawable(skin.ht9BgResId[4]);

        CloudDialerActivity.setBgAndScalType(mView_h_num5, skinCommonData);
        mView_h_num5.setImageDrawable(skin.ht9BgResId[5]);

        CloudDialerActivity.setBgAndScalType(mView_h_num6, skinCommonData);
        mView_h_num6.setImageDrawable(skin.ht9BgResId[6]);

        CloudDialerActivity.setBgAndScalType(mView_h_num7, skinCommonData);
        mView_h_num7.setImageDrawable(skin.ht9BgResId[7]);

        CloudDialerActivity.setBgAndScalType(mView_h_num8, skinCommonData);
        mView_h_num8.setImageDrawable(skin.ht9BgResId[8]);

        CloudDialerActivity.setBgAndScalType(mView_h_num9, skinCommonData);
        mView_h_num9.setImageDrawable(skin.ht9BgResId[9]);

        CloudDialerActivity.setBgAndScalType(mView_h_num_dial, skinCommonData);
        mView_h_num_dial.setImageDrawable(skin.hcallResId);

        CloudDialerActivity.setBgAndScalType(mView_h_num_star, skinCommonData);
        mView_h_num_star.setImageDrawable(skin.hstarResId);

        CloudDialerActivity.setBgAndScalType(mView_h_num_del, skinCommonData);
        mView_h_num_del.setImageDrawable(skin.hdelResId);

        CloudDialerActivity.setBgAndScalType(mView_h_num_sms, skinCommonData);
        mView_h_num_sms.setImageDrawable(skin.hsmsResId);

        mBtnHDialTextShowArea.setTextColor(skinCommonData.barTitleColor);
    }

    private AnimationListener mAnimationBeforeFreshListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
//            mActivity.sendMessageToHandler(Constants.FRESH_ON_BACKGROUND);

            if (mIsT9) {
                mQwertyBoard.clearAnimation();
                mQwertyBoard.setVisibility(View.GONE);
                mT9Board.setVisibility(View.VISIBLE);
            } else {
                mT9Board.clearAnimation();
                mT9Board.setVisibility(View.GONE);
                mQwertyBoard.setVisibility(View.VISIBLE);
            }
//            inputRequestFocus();
        }
    };

    private void keyPressed(int keyCode) {
        // removeCursorBlinking();
        KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
        if (mIsT9) {
            mDigits_h.onKeyDown(keyCode, event);
        } else {
            mDigits_h_words.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void onClick(View v) {

        int viewId = v.getId();
//        if (mActivity.mListStatus == CloudDialerActivity.ListStatus.ENUM_SHOW_CALL_LOG
////                && (mActivity.mBatchOperationType & 1 << Constants.BATCH_OPERATION_DELETE) != 0
//                ) {
//            new AlertDialog.Builder(mActivity)
//                    .setMessage("批量删除通话记录时不可查询!")
//                    .setNegativeButton("确定", null)
//                    .create().show();
//            return;
//
//        }else{
            if (KeyboardVertFrag.mViewIdKeyCodeMap != null
                    && KeyboardVertFrag.mViewIdKeyCodeMap.containsKey(viewId)) {
                v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                keyPressed(KeyboardVertFrag.mViewIdKeyCodeMap.get(viewId));
                return;
//            }
        }

        switch (v.getId()) {
            case R.id.btn_displayt9orwords:
                if (mSkinCommon != null) {
//                    mActivity.playPhone(mActivity.player, mSkinCommon.def_ussd, mActivity.ussdPlayerId, mSkinCommon.ussd);
                }

                mIsT9 = !mIsT9;
                switchKeyboard(mIsT9, mSkin, mSkinCommon, true);

                if (!mIsT9) {
                    if (mAnimationVerticalAllwordVisible == null) {
                        mAnimationVerticalAllwordVisible = AnimationUtils.loadAnimation(mActivity,
                                R.anim.slide_vertical_words_visible);
                        mAnimationVerticalAllwordVisible.setAnimationListener(mAnimationBeforeFreshListener);
                    }

                    mQwertyBoard.setVisibility(View.VISIBLE);
                    mQwertyBoard.startAnimation(mAnimationVerticalAllwordVisible);
                } else {
                    if (mIsDisplayUnderPanel) {
                        if (mAnimationVerticalAllwordGone == null) {
                            mAnimationVerticalAllwordGone = AnimationUtils.loadAnimation(mActivity,
                                    R.anim.slide_vertical_words_gone);
                            mAnimationVerticalAllwordGone.setAnimationListener(mAnimationBeforeFreshListener);
                        }
                        mQwertyBoard.startAnimation(mAnimationVerticalAllwordGone);
                    }
                }

                mPreferenceUtil.save(PreferenceUtil.IS_T9, mIsT9);
                if (!mIsDisplayUnderPanel) {
                    mIsDisplayUnderPanel = true;
//                    mDisplayUnderPanelButton.setText("▼");
                }

                break;
            case R.id.isdisplay_dialpadunder:
//                if (skin != null) {
//                    playPhone(player, skin.lock, lockPlayerId);
//                }
                //: vert keyboard
              /*  mIsDisplayUnderPanel = !mIsDisplayUnderPanel;
                if (mIsDisplayUnderPanel) {
                    mDisplayUnderPanelButton.setText("▼");
                    if (mIsT9) {
                        mQwertyBoard.setVisibility(View.GONE);
                        mT9Board.setVisibility(View.VISIBLE);
                    } else {
                        mT9Board.setVisibility(View.VISIBLE);
                        mQwertyBoard.setVisibility(View.VISIBLE);
                    }
                } else {
                    mDisplayUnderPanelButton.setText("▲");
                    mT9Board.setVisibility(View.GONE);
                    mQwertyBoard.setVisibility(View.GONE);
                }*/
                if(mActivity.mListStatus == CloudDialerActivity.ListStatus.ENUM_SHOW_CLOUD_CONTACT){
//                    mActivity.showButtonWindow(true);
                }else{
//                    mActivity.showButtonWindow(false);
                }

                break;
            case R.id.dial_btn_h_dial:
            case R.id.dial_btn_h_send_msg:
                mActivity.onClick(v);
                break;
            case R.id.dial_btn_h_delete:
                v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                keyPressed(KeyEvent.KEYCODE_DEL);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(v.getId()) {
            case R.id.dial_btn_num_h_0:
            case R.id.dial_btn_num_h_1:
            case R.id.dial_btn_num_h_2:
            case R.id.dial_btn_num_h_3:
            case R.id.dial_btn_num_h_4:
            case R.id.dial_btn_num_h_5:
            case R.id.dial_btn_num_h_6:
            case R.id.dial_btn_num_h_7:
            case R.id.dial_btn_num_h_8:
            case R.id.dial_btn_num_h_9:
            case R.id.dial_btn_h_delete:
            case R.id.dial_btn_h_xing:
//                mActivity.onTouch(v, event);
                break;
        }
        return false;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // HEIHEI Auto-generated method stub
        return false;
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.dial_btn_h_delete:
                if (mDigits_h.getText().length() > 0) {
                    mDigits_h.setText("");
                }
                break;
            case R.id.dial_btn_h_xing:
                keyPressed(KeyEvent.KEYCODE_POUND);
                break;
            case R.id.dial_btn_num_h_0:
                keyPressed(KeyEvent.KEYCODE_PLUS);
                break;
            case R.id.dial_btn_h_dial:
            case R.id.dial_btn_h_send_msg:

            case R.id.dial_btn_num_h_1:
            case R.id.dial_btn_num_h_2:
            case R.id.dial_btn_num_h_3:
            case R.id.dial_btn_num_h_4:
            case R.id.dial_btn_num_h_5:
            case R.id.dial_btn_num_h_6:
            case R.id.dial_btn_num_h_7:
            case R.id.dial_btn_num_h_8:
            case R.id.dial_btn_num_h_9:
//                mActivity.onLongClick(v);
                break;
            default:
                break;
        }
        return true;
    }

    public void setKeypadInVisible() {
        if ((mT9Board.getVisibility() == View.VISIBLE
                || mQwertyBoard.getVisibility() == View.VISIBLE)
                && mIsDisplayUnderPanel) {
//            mDisplayUnderPanelButton.setText("▲");
            mT9Board.setVisibility(View.GONE);
            mQwertyBoard.setVisibility(View.GONE);
            mIsDisplayUnderPanel = false;
        }
    }

    public void setArea(String area) {
        if (mIsT9) {
            mBtnHDialTextShowArea.setText(area);
        }
    }
}
